package com.example.music.controller;


import com.example.commonutils.R;
import com.example.music.service.DownloadService;
import com.example.music.utils.ConstantPropertiesUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName DownloadController
 * @Author: wx
 * @Create: 2022-10-08  20:14
 * @Description TODO
 * @Version: 1.0
 **/
@RestController
@Api(description = "下载歌曲")
public class DownloadController {
    @Autowired
    private DownloadService service;

    @Autowired
    private RedisTemplate<String,String> template;

    @ApiOperation("下载成功")
    @GetMapping("/download")
    public void downLoadFile(HttpServletRequest request, HttpServletResponse response, @RequestParam("song") String song) {
        service.downLoadFile(request, response,song);
        //return R.success("下载成功");
    }

    @GetMapping("/download1")
    public byte[] downloadSong(@RequestParam("song") String song) throws UnsupportedEncodingException {
        System.out.println(song);
        song = "/song/周杰伦-稻香.mp3";
        String strBin =  service.downloadSong(song);
        //template.opsForValue().set("1",strBin,5, TimeUnit.MINUTES);
        return strBin.getBytes("ISO-8859-1");
    }


    public String downloadSong1(@RequestParam("song") String song) throws UnsupportedEncodingException {
        System.out.println(song);
        //song = "/song/周杰伦-稻香.mp3";
        String strBin =  service.downloadSong(song);
        //template.opsForValue().set("1",strBin,5, TimeUnit.MINUTES);
        return strBin;
    }

    @ApiOperation("未完善，误使用")
    @GetMapping("/download/redis")
    public String downloadForRedis(@RequestParam("song") String song) {
        String strBin = template.opsForValue().get("1");
        System.out.println(strBin);
        return strBin;
    }

}
